﻿using System;
using System.Collections.Generic;
using System.Runtime.Remoting.Messaging;
using MySql.Data.MySqlClient;
using OrionApplication.OrionLib.Orion;

namespace OrionApplication.OrionLib.Dao
{
    public class DaoClient : DaoBase
    {

        public DaoClient()
            : base()
        {
            this.TableName = "client";
            this.ViewName = "vclient";
        }

        public Client SelectById(Int32 OrionId)
        {
            lParam.Clear();
            Client retour = new Client();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionId=@orionid";

            lParam.Add(new MySqlParameter("@OrionId", OrionId));

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            if (dr.Read())
                retour = ChargeFromReader(dr);
            dr.Close();
            return retour;
        }

        public List<Client> SelectAllActif()
        {
            lParam.Clear();
            List<Client> retour = new List<Client>();
            String req = this.DebutSelect
            + this.TableName
            + " WHERE OrionEtat=1";

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        public List<Client> SelectAll()
        {
            lParam.Clear();
            List<Client> retour = new List<Client>();
            String req = this.DebutSelect
            + this.TableName;

            MySqlDataReader dr = this.ExecuteQueryReader(req, lParam);

            while (dr.Read())
                retour.Add(ChargeFromReader(dr));
            dr.Close();

            return retour;
        }

        private Client ChargeFromReader(MySqlDataReader dr)
        {
            int i = 0;
            Client retour = new Client();
            retour.OrionId = dr.GetInt32("OrionId");

            i++;
            if (dr.IsDBNull(i))
                retour.FidelCodeBarre = String.Empty;
            else
                retour.FidelCodeBarre = dr.GetString("FidelCodeBarre");
            i++;
            if (dr.IsDBNull(i))
                retour.OrionQualiteId = 0;
            else
                retour.OrionQualiteId = dr.GetInt32("OrionQualiteId");
            i++;
            if (dr.IsDBNull(i))
                retour.Nom = String.Empty;
            else
                retour.Nom = dr.GetString("Nom");
            i++;
            if (dr.IsDBNull(i))
                retour.Prenom = String.Empty;
            else
                retour.Prenom = dr.GetString("Prenom");
            i++;
            if (dr.IsDBNull(i))
                retour.Adresse = String.Empty;
            else
                retour.Adresse = dr.GetString("Adresse");
            i++;
            if (dr.IsDBNull(i))
                retour.CP = String.Empty;
            else
                retour.CP = dr.GetString("CP");
            i++;
            if (dr.IsDBNull(i))
                retour.Ville = String.Empty;
            else
                retour.Ville = dr.GetString("Ville");
            i++;
            if (dr.IsDBNull(i))
                retour.Pays = String.Empty;
            else
                retour.Pays = dr.GetString("Pays");
            i++;
            if (dr.IsDBNull(i))
                retour.Mail = String.Empty;
            else
                retour.Mail = dr.GetString("Mail");
            i++;
            if (dr.IsDBNull(i))
                retour.Telephone = String.Empty;
            else
                retour.Telephone = dr.GetString("Telephone");
            i++;
            if (dr.IsDBNull(i))
                retour.Portable = String.Empty;
            else
                retour.Portable = dr.GetString("Portable");
            i++;
            if (dr.IsDBNull(i))
                retour.DateNaissance = null;
            else
                retour.DateNaissance = dr.GetDateTime("DateNaissance");
            i++;
            if (dr.IsDBNull(i))
                retour.Commentaire = String.Empty;
            else
                retour.Commentaire = dr.GetString("Commentaire");

            retour.OrionCrea = dr.GetDateTime("OrionCrea");
            retour.OrionMaj = dr.GetDateTime("OrionMaj");
            retour.OrionUserId = dr.GetInt32("OrionUserId");
            retour.OrionEtat = dr.GetBoolean("OrionEtat");

            return retour;
        }

        public Client Update(Client oe)
        {
            lParam.Clear();
            oe.OrionMaj = DateTime.Now;

            Client retour = new Client();
            String req = "UPDATE " + TableName + " SET FidelCodeBarre=@FidelCodeBarre,OrionQualiteId=@OrionQualiteId,Nom=@Nom,Prenom=@Prenom,Adresse=@Adresse,CP=@CP,Ville=@Ville,Pays=@Pays,Mail=@Mail,Telephone=@Telephone,Portable=@Portable,DateNaissance=@DateNaissance,Commentaire=@Commentaire,OrionUserId=@OrionUserId, OrionMaj=@OrionMaj WHERE OrionId=@OrionId";

            lParam.Add(new MySqlParameter("@FidelCodeBarre", oe.FidelCodeBarre));
            lParam.Add(new MySqlParameter("@OrionQualiteId", oe.OrionQualiteId));
            lParam.Add(new MySqlParameter("@Nom", oe.Nom));
            lParam.Add(new MySqlParameter("@Prenom", oe.Prenom));
            lParam.Add(new MySqlParameter("@Adresse", oe.Adresse));
            lParam.Add(new MySqlParameter("@CP", oe.CP));
            lParam.Add(new MySqlParameter("@Ville", oe.Ville));
            lParam.Add(new MySqlParameter("@Pays", oe.Pays));
            lParam.Add(new MySqlParameter("@Mail", oe.Mail));
            lParam.Add(new MySqlParameter("@Telephone", oe.Telephone));
            lParam.Add(new MySqlParameter("@Portable", oe.Portable));
            lParam.Add(new MySqlParameter("@DateNaissance", oe.DateNaissance));
            lParam.Add(new MySqlParameter("@Commentaire", oe.Commentaire));

            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));
            lParam.Add(new MySqlParameter("@OrionId", oe.OrionId));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(oe.OrionId);

            return retour;
        }

        public Client Insert(Client oe)
        {
            lParam.Clear();
            oe.OrionCrea = DateTime.Now;
            oe.OrionMaj = DateTime.Now;

            Client retour = new Client();
            String req = "INSERT INTO " + TableName + "(OrionId,FidelCodeBarre,OrionQualiteId,Nom,Prenom,Adresse,CP,Ville,Pays,Mail,Telephone,Portable,DateNaissance,Commentaire,OrionEtat, OrionUserId, OrionCrea, OrionMaj)  VALUES(NULL,@FidelCodeBarre,@OrionQualiteId,@Nom,@Prenom,@Adresse,@CP,@Ville,@Pays,@Mail,@Telephone,@Portable,@DateNaissance,@Commentaire,@OrionEtat,@OrionUserId,@OrionCrea,@OrionMaj)";
            lParam.Add(new MySqlParameter("@FidelCodeBarre", oe.FidelCodeBarre));
            lParam.Add(new MySqlParameter("@OrionQualiteId", oe.OrionQualiteId));
            lParam.Add(new MySqlParameter("@Nom", oe.Nom));
            lParam.Add(new MySqlParameter("@Prenom", oe.Prenom));
            lParam.Add(new MySqlParameter("@Adresse", oe.Adresse));
            lParam.Add(new MySqlParameter("@CP", oe.CP));
            lParam.Add(new MySqlParameter("@Ville", oe.Ville));
            lParam.Add(new MySqlParameter("@Pays", oe.Pays));
            lParam.Add(new MySqlParameter("@Mail", oe.Mail));
            lParam.Add(new MySqlParameter("@Telephone", oe.Telephone));
            lParam.Add(new MySqlParameter("@Portable", oe.Portable));
            lParam.Add(new MySqlParameter("@DateNaissance", oe.DateNaissance));
            lParam.Add(new MySqlParameter("@Commentaire", oe.Commentaire));
            lParam.Add(new MySqlParameter("@OrionEtat", oe.OrionEtat));
            lParam.Add(new MySqlParameter("@OrionUserId", ((OrionUser)CallContext.GetData(OrionConfBase.OrionUserConnected)).OrionId));
            lParam.Add(new MySqlParameter("@OrionCrea", oe.OrionCrea));
            lParam.Add(new MySqlParameter("@OrionMaj", oe.OrionMaj));

            this.ExecuteNonQuery(req, lParam);
            retour = this.SelectById(Convert.ToInt32(this.LastInsertId));

            return retour;
        }
    }
}